/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Di2Factory;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.umlutils.ICustomAppearence;

/**
 * Section to customize port label display
 * Given UML2 superstructure, label for a port is:
 * <ul><li>its name</li>
 * <li>its type</li>
 * <li>its multiplicity</li>
 * </ul>.
 * 
 * @author Remi Schnekenburger
 */
public class PortAppearenceSection extends PropertyViewSection {
	
	/**
	 * 
	 */
	private GraphElement graphElement;
	
	/**
	 * 
	 */
	private Button displName, displType, displMutliplicity;
	
	/**
	 * 
	 */
	private SelectionListener displNameListener, displTypeListener, displMutliplicityListener;

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		displName = getWidgetFactory().createButton(composite, "Name", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		displName.setLayoutData(data);
		displName.addSelectionListener(displNameListener = new AppearenceSelectionListener(ICustomAppearence.DISP_NAME));

		displType = getWidgetFactory().createButton(composite, "Type", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(displName, 85);
		data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		displType.setLayoutData(data);
		displType.addSelectionListener(displTypeListener = new AppearenceSelectionListener(ICustomAppearence.DISP_TYPE));
		
		displMutliplicity = getWidgetFactory().createButton(composite, "Multiplicity", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(displType, 85);
		data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		displMutliplicity.setLayoutData(data);
		displMutliplicity.addSelectionListener(displMutliplicityListener = new AppearenceSelectionListener(ICustomAppearence.DISP_MULTIPLICITY));
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	public void refresh() {
		if(!displName.isDisposed()){
			
			// if no graph element are associated, there should not be the appearence section
			if(graphElement == null) {
				return;
			}
			// get Property associated to this element
			com.cea.papyrus.diagraminterchange2.di2.Property prop = graphElement.getProperty(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
			if(prop == null) {
				// creates a default one
				prop = Di2Factory.eINSTANCE.createProperty();
				graphElement.getProperty().add(prop);
				prop.setKey(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
				prop.setValue(""+ICustomAppearence.DEFAULT_UML_PROPERTY);
			}

			int value = Integer.parseInt(prop.getValue());

			displName.setSelection((value & ICustomAppearence.DISP_NAME) != 0);
			displType.setSelection((value & ICustomAppearence.DISP_TYPE) != 0);
			displMutliplicity.setSelection((value & ICustomAppearence.DISP_MULTIPLICITY) != 0);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	/**
	 * 
	 * 
	 * @param part 
	 * @param selection 
	 */
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();

		if(input instanceof AbstractGraphicalEditPart) {
			graphElement = ((GraphElement)((AbstractGraphicalEditPart) input).getModel());
		} else {
			graphElement = null;
		}
		refresh();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(displName!=null && !displName.isDisposed())
			displName.removeSelectionListener(displNameListener);
		if(displType!=null && !displType.isDisposed())
			displType.removeSelectionListener(displTypeListener);
		if(displMutliplicity!=null && !displMutliplicity.isDisposed())
			displMutliplicity.removeSelectionListener(displMutliplicityListener);
	}

	/**
	 * 
	 */
	private class AppearenceSelectionListener implements SelectionListener {

		/**
		 * the constant which refers the element to display or not.
		 */
		private int style;

		/**
		 * Default Constructor.
		 * 
		 * @param style 
		 */
		public AppearenceSelectionListener(int style) {
			this.style = style;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetDefaultSelected(SelectionEvent e) {

		}

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetSelected(SelectionEvent e) {
			Button button = (Button)e.getSource();
			boolean isSelected = button.getSelection();

			if(graphElement == null) {
				return;
			}
			com.cea.papyrus.diagraminterchange2.di2.Property prop = graphElement.getProperty(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
			if(prop == null) {
				Debug.error(null, "property is null", 7);
			}
			int value = Integer.parseInt(prop.getValue());

			if(isSelected) {
				if((value & style) == 0) {
					prop.setValue(""+(value | style));
				}
			} else {
				if((value & style) != 0) {
					prop.setValue(""+(value & ~style));
				}
			}
			forceToSave();
			refresh();
		}
	}
}

